package com.pactera.madp.cp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.taskpublish.ReadEmployeeDTO;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.entity.taskpublish.ReadCountEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.taskpublish.TaskReadCountMapper;
import com.pactera.madp.cp.service.ITaskReadCountService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @date: 2021/9/7 11:33
 * @description:
 */
@Service
public class TaskReadCountServiceImpl extends ServiceImpl<TaskReadCountMapper, ReadCountEntity> implements ITaskReadCountService {

    @Autowired
    private IWorkEmployeeService workEmployeeService;

    @Override
    public R saveEmployeeList(ReadEmployeeDTO readEmployeeDTO) {
        ReadCountEntity entity = new ReadCountEntity();
        entity.setTaskId(readEmployeeDTO.getTaskId());
        entity.setChatId(readEmployeeDTO.getChatId());
        entity.setCorpId(CorpContextHolder.getCorpId());

        //获取群成员id列表
        List<String> userlist = workEmployeeService.list(Wrappers.<WorkEmployeeEntity>lambdaQuery()
                        .eq(WorkEmployeeEntity::getCorpId, CorpContextHolder.getCorpId())
                        .in(WorkEmployeeEntity::getId, readEmployeeDTO.getUserList()))
                        .stream().map(WorkEmployeeEntity::getWxUserId).collect(Collectors.toList());
        Object json = JSONObject.toJSON(userlist);
        entity.setReadEmployee(json.toString());
        return this.save(entity)?R.ok("保存成功"):R.failed("保存失败");
    }
}
